package com.hns.can.mina.handle;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

/**
 * @版权：HNS 版权所有 (c) 2013
 * @author:Fisher
 * @version Revision 1.0.0
 * @email:zhuangcaijin@126.com
 * @see:
 * @创建日期：2015-10-9
 * @功能说明：处理与Flex认证的线程类
 * @begin
 * @修改记录:
 * @修改后版本          修改人      	修改内容
 * @2015-10-9  	         Fisher        	创建
 * @end
 */
public class PolicyThread implements Runnable {
    private final String policy_xml = "<policy-file-request/>";
    private final String cross_xml  = "<?xml version=\"1.0\"?><cross-domain-policy><site-control permitted-cross-domain-policies=\"all\"/><allow-access-from domain=\"*\" to-ports=\"*\"/></cross-domain-policy>\0";
    private Socket       socket;
    
    public PolicyThread(Socket socket) {
        this.socket = socket;
    }
    
    @Override
    public void run() {
        try {
            // 接收并发送Flex安全验证请求
            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            PrintWriter pw = new PrintWriter(socket.getOutputStream());
            char[] by = new char[22];
            br.read(by, 0, 22);
            String s = new String(by);
            if (policy_xml.equals(s)) {
                pw.print(cross_xml);
                pw.flush();
                br.close();
                pw.close();
                socket.close();
            }
        } catch (IOException e) {
            if (!socket.isClosed()) {
                try {
                    socket.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            throw new RuntimeException("执行policy认证时发生异常", e);
        }
    }
    
}
